#!/bin/zsh -f

# This is a pre-command function that notifies the user when the command (provided as an argument)
# completes. It attempts to use growlnotify if present, otherwise it prints a message. If the command
# returns anything other than 0 (i.e., it fails), the growl window remains sticky. 

# A command completed that returns 0 will terminate with a non-sticky growlnotify window. Use the 
# command snotify if you want that one to be sticky as well.

if [[ $# == 0 ]]; then
	print ""
	print "\e[1m Usage: \e[0m $0 command-string"
	print ""
	return 42
fi

# If growlnotify is present, use it to notify

 "$@" 
  returnvalue=$?

if [[ -x $(which growlnotify) && -z $SSH_TTY ]]; then


	if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
		icon_array=( --appIcon iTerm.app )
	else  
		icon_array=()
	fi

	if [[ "$returnvalue" == 0 ]]; then

		growlnotify  $icon_array -m "$1 completed."  

	else
 
		growlnotify -s $icon_array -m "$1 failed."  

	fi

else

	if [[ "$returnvalue" == 0 ]]; then

		print "" ; print "$1 completed."  ; print ""

	else

		print "" ; print "$1 failed."  ; print "" 

	fi

fi

return "$returnvalue"